/**
 * Copyright (c) 2008, Piercarlo Slavazza
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * The name of Piercarlo Slavazza cannot be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Piercarlo Slavazza ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Piercarlo Slavazza BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.libapml.serializers;

import java.io.File;
import java.io.OutputStream;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import com.libapml.ApmlIndex;
import com.libapml.apml.Apml;
import com.libapml.apml.Concept;
import com.libapml.util.Helper;

public class ApmlSerializer {

	private Apml apmlModel;
	private ApmlIndex index;
	
	private JAXBContext context;
	private Marshaller marshaller;
	
	public ApmlSerializer(ApmlIndex index) throws JAXBException {
		
		Helper.checkNotNull(index);
		
		this.index = index;
		
		buildApml();
		
		context = JAXBContext.newInstance(Apml.class);
		marshaller = context.createMarshaller();
		marshaller.setProperty("jaxb.formatted.output", true);
	}
	
	public boolean next(com.libapml.Concept concept, int position) {
		
		return (position < 20);
		
	}
	
	private void buildApml() {
		
		apmlModel = new Apml();
		
		List<Concept> apmlConcepts = apmlModel.getImplicitConcepts();
		int count = 0;
		for (final com.libapml.Concept concept : index.getSortedConcepts()) {
			if (next(concept, count++)) {
				apmlConcepts.add(new Concept() {{
					key = concept.getTerm();
					value = concept.getTfidfMean();
				}});
			} else
				break;
		}
	}
	
	public void serialize(File output) throws JAXBException {
	
		marshaller.marshal(apmlModel, output);
		
	}
	
	public void serialize(OutputStream output) throws JAXBException {
		
		marshaller.marshal(apmlModel, output);
		
	}
	
}
